package com.decoration.basic.controller;

import com.alibaba.druid.util.StringUtils;
import com.decoration.basic.BasicController;
import com.decoration.basic.entity.DecDailyOutMsg;
import com.decoration.basic.entity.DecorationDailyInf;
import com.decoration.basic.entity.DecorationUser;
import com.decoration.basic.entity.JsonResult;
import com.decoration.basic.findCondition.DecDailyFindCondition;
import com.decoration.basic.findCondition.DecDailyListCondition;
import com.decoration.basic.mapper.DecPictureMapper;
import com.decoration.basic.po.DecCtctionInfVo;
import com.decoration.basic.service.DecDailyService;
import com.decoration.basic.service.DecUserService;
import com.decoration.basic.util.DateFormatUtils;
import com.decoration.basic.util.Global;
import com.decoration.basic.util.SnowFlake;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class DailyController extends BasicController{

	private static Logger log = LoggerFactory.getLogger(DailyController.class);

	@Autowired
	private DecDailyService decDailyService;

	// 用户获取日报列表
	@RequestMapping(value = "/daily/list", method = RequestMethod.POST)
	public JsonResult dailylist(JsonResult jsonResult, @RequestBody DecDailyListCondition findCondition, HttpSession session) {

		//校验模糊查询条件
		if (StringUtils.isEmpty(findCondition.getCtcName())){
			log.info("模糊查询条件为空为空");
		}

		//添加分页信息
		setPagerInfo(findCondition);
		// 获取列表信息
		List<DecDailyOutMsg> list = new ArrayList<>();
		int sum = 0;
		try {
			list = decDailyService.searchDailyList(findCondition);
			PageInfo<DecDailyOutMsg> pageInfo = new PageInfo(list);
			sum = Integer.parseInt(String.valueOf(pageInfo.getTotal()));
		} catch (Exception e) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setCount(sum);
			jsonResult.setMsg("获取日报列表失败!");
			jsonResult.setData(list);
			return jsonResult;
		}
		if (list == null || list.size() <1 ){
            jsonResult.setCode(JsonResult.SUCCESS_CODE);
            jsonResult.setCount(sum);
            jsonResult.setMsg("获取日报列表为空!");
			jsonResult.setData(list);
            return jsonResult;
        }

		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setCount(sum);
		jsonResult.setMsg("获取日报列表成功!");
		jsonResult.setData(list);
		return jsonResult;
	}


	/**
	 * 用户获取日报内容
	 * @param jsonResult
	 * @param findCondition
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/daily/dailyId", method = RequestMethod.POST)
	public JsonResult dailybyCurdate(JsonResult jsonResult, @RequestBody DecorationDailyInf findCondition, HttpServletRequest request) {

		//获取日报信息
		DecorationDailyInf list = new DecorationDailyInf();
		try {
			list = decDailyService.findById(findCondition.getDailyId(),"");
		} catch (Exception e) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("查询数据失败，请联系管理员!");
			jsonResult.setCode(0);
			jsonResult.setData(list);
			return jsonResult;
		}

		if (list == null ) {
			jsonResult.setCode(JsonResult.SUCCESS_CODE);
			jsonResult.setMsg("获取日报信息为空!");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setCount(1);
		jsonResult.setMsg("获取日报信息成功!");
		jsonResult.setData(list);
		return jsonResult;
	}

}
